<?php

declare(strict_types=1);

namespace App\Controller;

use App\Annotation\LogAnnotation;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\Industry;
use App\Service\TopicService;
use Hyperf\Validation\Rule;
use Hyperf\DbConnection\Db;
Use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

class TopicController extends AbstractController
{

    /**
     * @Inject
     * @var TopicService
     */
    private $topicService;

    public function list()
    {
        $params = $this->request->inputs(['key_word','is_show','ids','current','size','type']);
        $list = $this->paginate($this->topicService->get_list($params));
        return $this->jsonResponse->success($list, '获取列表成功');
    }


    public function list_all()
    {
        $params = $this->request->inputs(['key_word','is_show','ids','current','size','type']);
        $this->validation(
            $params,
            [
                'type' => [
                    'required',
                    'integer',
                    'min:1',
                    'max:2',
                ],
            ],
        );
        $list = $this->topicService->get_list($params)->get();
        return $this->jsonResponse->success($list, '获取列表成功');
    }

    public function detail()
    {
        $id = $this->request->input('id');
        $this->validation(
            ['id'=>$id],
            [
                'id' => [
                    'required',
                    'integer',
                    'min:0',
                    Rule::exists('fly_topic','id')->where(function ($query){
                        $query->whereNull('deleted_at');
                    }),
                ],
            ],
        );
        try{
            $this->topicService->info($id);
            return $this->jsonResponse->success($this->topicService->info($id),'信息返回');
        } catch (\Throwable $exception) {
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'不存在或已删除：'.$exception->getMessage());
        }
    }
}